package cc.customer.web.center.SalePhone;

import cc.customer.bigEmport.damain.BigEmp;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.*;
import cc.customer.common.utils.poi.POIUtils;
import cc.customer.phoneTask.domain.CPhoneTask;
import cc.customer.phoneTask.services.CPhoneServices;
import cc.customer.system.domain.MyUser;
import cc.customer.web.sys.controller.base.BaseController;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="SalePhone")
public class SalePhoneController extends BaseController {


    @Autowired
    private CPhoneServices cPhoneServices;


    /**创建电话任务--主页面**/
    @RequestMapping(value="loadSaleCreatephone")
    public String loadSaleCreatephone(){return "customer/center/salePhone/create_phone";}

    /**加载个人的电话任务**/
    @RequestMapping(value="loadSalePersonphone")
    public String loadSalePersonphone(){return "customer/center/salePhone/persn_phone";}
    /**上传导入文件**/
    @RequestMapping(value="loadSaleAddTask")
    public String loadSaleAddTask(){return "customer/center/salePhone/addTask";}
    /**记载详细页面**/
    @RequestMapping(value="loadSaleTaskDetail")
    public String loadSaleTaskDetail(){return "customer/center/salePhone/taskDetail";}
    /**记载个人详细页面**/
    @RequestMapping(value="loadSalePersonTaskDetail")
    public String loadSalePersonTaskDetail(){return "customer/center/salePhone/personTask";}

    /**分配详细页面**/
    @RequestMapping(value = "loadSaleFenTask")
    public String loadSaleFenTask(){return "customer/center/salePhone/FenpeiTask";}

    /**统计页面**/
    @RequestMapping(value = "loadSaleDataNum")
    public String loadSaleDataNum(){return "customer/center/salePhone/data_num";}

    /**设置页面**/
    @RequestMapping(value = "loadSaleSetPhone")
    public String loadSaleSetPhone(){return "customer/center/salePhone/setTaskPhone";}
    /**选择人员**/
    @RequestMapping(value = "loadSaleSetUser")
    public String loadSaleSetUser(){return "customer/center/salePhone/setSelectUser";}
    /**新增个人用户**/
    @RequestMapping(value = "loadAddCustomer")
    public String loadAddCustomer(){return "customer/center/salePhone/addMyCustomer";}


    /**下载导入模板**/
    @RequestMapping(value = "downloadModal" ,method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo downloadModal(){
        try {
            CPhoneTask task = new CPhoneTask();
            task.setSpName("张三");
            task.setSpPhone("15253130180");
            List<CPhoneTask> list = new ArrayList<>();
            list.add(task);
            return FileUtils.createExcelByPOIKit("电话任务模板", list, CPhoneTask.class);
        } catch (Exception e) {
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }
    /**导入模板数据**/
    @RequestMapping(value="/uploadFile",method= RequestMethod.POST,produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public ResponseBo uploadPhoneTask(MultipartFile[] file){
        if (file.length<=0){
            return ResponseBo.error("上传文件为空");
        }
        String  FilePath = Constant.PHONETASK_FILE_PATH;
        return FileUploadUtil.excuteFiles(FilePath,file);
    }
    /**批量导入数据**/
    @RequestMapping(value = "addBatchPhoneTask",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo addBatchPhoneTask(String fileUrl,String title){
        try {
            MyUser user = this.getCurrentUser();
            List<CPhoneTask> list = new ArrayList<>();
            File file = new File(fileUrl);
            Workbook rwb= new XSSFWorkbook(new FileInputStream(file));
            Sheet sheet = rwb.getSheetAt(0);
            int rows = sheet.getLastRowNum();//获取所有得行
            String uuid = Utils.getUUID();
            for (int i = 1; i <= rows; i++) {
                CPhoneTask task = new CPhoneTask();
                Row row = sheet.getRow(i);
                //获取客户名称
                Cell Phone = row.getCell(1);
                if (Phone!=null){
                    String opone =  POIUtils.getCellValue(Phone);
                    task.setSpPhone(opone);
                }else{
                    continue;
                }
                Cell name = row.getCell(0);
                if (name != null){
                    String spName = POIUtils.getCellValue(name);
                    task.setSpName(spName);
                }else{
                    task.setSpName("无姓名");
                }
                task.setSpBatch(title);
                task.setSpUuid(uuid);
                task.setSpUuids(Utils.getUUID());
                task.setSpCreator(user.getUserUuid());
                task.setSpCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
                task.setSpStatusFen("1");
                list.add(task);
            }
            return cPhoneServices.batchImpoertData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("解析Excel失败，请联系网站管理员！");
        }
    }
    /**初始化加载所有的电话信息**/
    @RequestMapping(value = "getAllCphoneTask",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo getAllCphoneTasks(CPhoneTask cPhoneTask){
        List<CPhoneTask> list  = cPhoneServices.getAllCphoneTasks(cPhoneTask);
        return ResponseBo.ok(list);
    }
    /**初始化加载所有的电话信息**/
    @RequestMapping(value = "getAllCphonePersonTask",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo getAllCphonePersonTask(CPhoneTask cPhoneTask){
        MyUser user = this.getCurrentUser();
        cPhoneTask.setSpRecer(user.getUserUuid());
        List<CPhoneTask> list  = cPhoneServices.getAllCphoneTasks(cPhoneTask);
        return ResponseBo.ok(list);
    }

    /**初始化批次的详细信息**/
    @RequestMapping(value = "getAllCphoneTaskByBatch",method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getAllCphoneTaskByBatch(CPhoneTask cPhoneTask, QueryRequest request){
       return  this.selectByPageNumSize(request,()-> this.cPhoneServices.getCphoneList(cPhoneTask));
    }

    /**初始化批次的详细信息**/
    @RequestMapping(value = "getAllCphoneTaskMyByBatch",method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getAllCphoneTaskMyByBatch(CPhoneTask cPhoneTask, QueryRequest request){
        MyUser user = this.getCurrentUser();
        cPhoneTask.setSpRecer(user.getUserUuid());
        return  this.selectByPageNumSize(request,()-> this.cPhoneServices.getCphoneList(cPhoneTask));
    }

    /**初始化批次的详细信息**/
    @RequestMapping(value = "getStatisTaskByBatch",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo getStatisTaskByBatch(String uuid){
        return   this.cPhoneServices.selectStartis(uuid);
    }

    /**批量分配的详细信息**/
    @RequestMapping(value = "FenTaskByBatch",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo FenTaskByBatch(CPhoneTask cPhoneTask){
        try {
            return   this.cPhoneServices.FenCphoneTasks(cPhoneTask);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error();
    }
    /**获取设置数据**/
    @RequestMapping(value = "getSetStatis",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo getSetStatis(String uuid){
        return   this.cPhoneServices.selectSetInfo(uuid);
    }
    /**转接客户**/
    @RequestMapping(value = "ZhuanTaskByBatch",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo ZhuanTaskByBatch(CPhoneTask cPhoneTask){
        try {
            return   this.cPhoneServices.updateTaskRecer(cPhoneTask);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseBo.error();
    }
    /**删除客户**/
    @RequestMapping(value = "deleteTaskByBatch",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo deleteTaskByBatch(String uuid){
        return   this.cPhoneServices.deleteTaskPhone(uuid);
    }

    /**移动完成**/
    @RequestMapping(value = "updateTaskPhoneByUuid",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo updateTaskPhone(CPhoneTask cPhoneTask){
        try {
            return this.cPhoneServices.updateTaskPhone(cPhoneTask);
        }catch (Exception e){
            return  ResponseBo.error();
        }
    }
    /**更新电话状态**/
    @RequestMapping(value = "updateTaskPhoneRecord",method = {RequestMethod.POST})
    @ResponseBody
    public ResponseBo updateTaskPhoneRecord(CPhoneTask cPhoneTask){
        cPhoneTask.setSpLastRecord(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
        return this.cPhoneServices.updateTaskPhoneRecord(cPhoneTask);
    }



}
